<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/5/21 0021
 * Time: 10:06
 */

namespace Wx\Controller;
use Think\Controller;
use Wx\Model\ForetasteModel;
use Wx\Model\shareModel;

class ForetasteEvaluateController extends CommonController
{
    // 评价列表
    public function index(){
        // 获取该商品所有的评价
        $g_id = intval(I('gid'));
        $count = M('foretaste_evaluate')->where('g_id = '.$g_id)->count();    //评价总数
        $list = M('foretaste_evaluate')->table(C('DB_PREFIX')."foretaste_evaluate as e")  //评价详情
        ->join(C('DB_PREFIX')."member as m ON m.id=e.u_id")
            ->field('e.*,m.nickname,m.avatar,m.hometown')
            ->where("e.g_id=".$g_id." AND state = 1")
            ->order('id desc')
            ->select();
        foreach ($list as $k => $v){
            if ($v['is_inco'] == 1){
                $list[$k]['nickname']='匿名';
            }
            $list[$k]['comm_num'] = M('evaluate_comment')->where('e_id = '.$v['id'])->count();  //评论数
            $list[$k]['pic'] = $var=explode(",",$v['pic']);              //处理评价图片
        }

        $this->assign('count',$count);
        $this->assign('list',$list);
        $this->display('evaluatelist');
    }// end fun index()

    // 商品评价详情
    public function commentdetail(){
        $e_id = intval(I('e_id'));
        $u_id = session('u_id');
        $e_info = M('foretaste_evaluate')->where('id = '.$e_id)->find();  //评价详情
        $e_info['focus'] = session('focus');//是否关注
        $e_info['goodstitle'] = M('foretaste_goods')->where('id = '.$e_info['g_id'])->getField('title');  //评价详情

        //辨别用户
        if ($u_id == $e_info['u_id']){
            $e_info['is_me'] = "true";
        }else{
            $e_info['is_me'] = "false";
        }

        $m_info = M('member')->where('id = '.$e_info['u_id'])->field('nickname,avatar,hometown')->find();      //评价用户详情
        if ($e_info['is_inco'] == 0){
            $m_info['nickname']='匿名';
        }
        $e_info['pic'] = $var=explode(",",$e_info['pic']);         //处理评价图片
        $comment = M('evaluate_comment')->table(C('DB_PREFIX')."evaluate_comment as c")  //评价-评论详情
        ->join(C('DB_PREFIX')."member as m ON m.id=c.send_id")
            ->field('c.*,m.nickname,m.avatar,m.hometown')
            ->where('e_id = '.$e_id)
            ->order('id desc')
            ->select();
        $count = M('evaluate_comment')->where('e_id = '.$e_id)->count();    //评论总数

        //微信分享
        $wxshare=new shareModel(C('WX_API.appid'),C('WX_API.secret'));
        $signPackage=$wxshare->getSignPackage();
        /*
        *
        array(6) {
          ["appId"] => string(18) "wx6a0daaedc4d00100"
          ["nonceStr"] => string(16) "5cKMHQADQTbRkv2f"
          ["timestamp"] => int(1527576256)
          ["url"] => string(59) "http://www.xiangtws.com/index.php/Wx/Prodetail/index/id/20/"
          ["signature"] => string(40) "a70ef78304bc4f5558439b65955119806e4fb3fe"
          ["rawString"] => string(205) "jsapi_ticket=HoagFKDcsGMVCIY2vOjf9raVdUJqZ1stwmkyXwhMyovuJhCTHjtpRnNxRJSVQ0UacAdrqz97vWdzjRU3Dp-YYw&noncestr=5cKMHQADQTbRkv2f*tamp=1527576256&url=http://www.xiangtws.com/index.php/Wx/Prodetail/index/id/20/"
        }
        *
        */		//dump($signPackage);
        $this->assign('signPackage',$signPackage);//微信数据
        //微信分享-end

        //未关注的存储到xcache中，程序轮询检测（关注后推送最近浏览的商品信息）-start
        if(session('focus')==0 && session('u_id')>0){
            $focusmsg=new \Wx\Controller\AutofocusmsgController();  //用绝对路径找
            $focusmsg->setxcache(session('u_id'),"来乡土味士，吃天然菜，评选扶贫大使！",'http://www.xiangtws.com'.$_SERVER['REQUEST_URI']);
        }//end if
        //未关注的存储到xcache中，程序轮询检测（关注后推送最近浏览的商品信息）-end

        $this->assign('u_id',$u_id);
        $this->assign('e_info',$e_info);
        $this->assign('m_info',$m_info);
        $this->assign('comment',$comment);
        $this->assign('count',$count);
        $this->display();
    }// end fun commentdetail()

    // 添加评价de评价
    public function addComment(){
        $u_id = session('u_id');
        if(IS_POST){
            $post = I('post.');
            $post['send_id'] = session('u_id'); //评论发送人id
            $post['addtime'] = time();
            $post['accept_id'] = M('foretaste_evaluate')->where('id = '.$post['e_id'])->getField('u_id'); //评论接收人id
            if($u_id != $post['accept_id']){
                $res = M('evaluate_comment')->add($post);
                if($res){
                    /*---------------------------------添加爱心值-start-------------------------------------*/
                    $helppoor = M("helppoor")->where("state = 1")->getField('id');//获取排行活动id
                    if (isset($helppoor)){
                        $is_new = M("foretaste_ranking")->where("u_id = {$post['accept_id']}  AND h_id = {$helppoor}")->find();//查看是否有排名记录
                        if (isset($is_new)){
                            $e_id = explode(',',$is_new['e_id']);//处理评论id
                            if (in_array($post['e_id'],$e_id)){
                                $eva_num = $is_new['eva_num']+1;//增加爱心值
                                M("foretaste_ranking")->where("id = {$is_new['id']}")->save(['eva_num'=>$eva_num]);
                            }//end if
                            $this->ajaxReturn(['state'=>0,'val'=>'提交成功'],'json');
                        }else{
                            $this->ajaxReturn(['val'=>'该评论不存在'],'json');
                        }//end if
                    }//end if
                    /*---------------------------------添加爱心值-end-------------------------------------*/
                }//end if
            }else{
                $this->error('无法评价自己');
            }

        }
    }// end fun addComment()


    // 点赞
    public function addLike(){
        $u_id = session('u_id');
        if(IS_POST){
            $e_id = intval(I('e_id'));
            $table = I('table');

            //区分评论与回复
            if ($table == 'foretaste_evaluate'){
                $Cu_id = M($table)->where('id = '.$e_id)->getField('u_id');//获取发布人id
            }else{
                $Cu_id = M("evaluate_comment")->where('id = '.$e_id)->getField('send_id');//获取发布人id
            }

            if ($Cu_id != $u_id){
                $like = M($table)->where('id = '.$e_id)->setInc('like_num',1);
                if ($like){
                    //区分评论与回复
                    if ($table == 'foretaste_evaluate'){
                        /*---------------------------------添加爱心值-start-------------------------------------*/
                        $helppoor = M("helppoor")->where("state = 1")->getField('id');//获取排行活动id
                        if (isset($helppoor)){
                            $is_new = M("foretaste_ranking")->where("u_id = {$Cu_id} AND h_id = {$helppoor}")->find();//查看是否有排名记录
                            if (isset($is_new)){
                                $Re_id = explode(',',$is_new['e_id']);//处理评论id
                                if (in_array($e_id,$Re_id)){
                                    M("foretaste_ranking")->where("id = {$is_new['id']}")->setInc('eva_num',1);
                                }//end if
                            }//end if
                        }//end if
                        /*---------------------------------添加爱心值-end-------------------------------------*/
                    }//end if
                }//end if
            }//end if
        }//end if
    }// end fun addLike()

    // 取消点赞
    public function cancleLike(){
        $u_id = session('u_id');
        if(IS_POST){
            $e_id = intval(I('e_id'));
            $table = I('table');
            //区分评论与回复
            if ($table == 'foretaste_evaluate'){
                $Cu_id = M($table)->where('id = '.$e_id)->getField('u_id');//获取发布人id
            }else{
                $Cu_id = M("evaluate_comment")->where('id = '.$e_id)->getField('send_id');//获取发布人id
            }
            if ($Cu_id != $u_id){
                $like = M($table)->where('id = '.$e_id)->setDec('like_num',1);
                if ($like){
                    //区分评论与回复
                    if ($table == 'foretaste_evaluate'){
                        /*---------------------------------添加爱心值-start-------------------------------------*/
                        $Cu_id = M("foretaste_evaluate")->where('id = '.$e_id)->getField('u_id');//获取发布人id
                        $helppoor = M("helppoor")->where("state = 1")->getField('id');//获取排行活动id
                        if (isset($helppoor)){
                            $is_new = M("foretaste_ranking")->where("u_id = {$Cu_id} AND h_id = {$helppoor}")->find();//查看是否有排名记录
                            if (isset($is_new)){
                                $Re_id = explode(',',$is_new['e_id']);//处理评论id
                                if (in_array($e_id,$Re_id)){
                                    M("foretaste_ranking")->where("id = {$is_new['id']}")->setDec('eva_num',1);
                                }//end if
                            }//end if
                        }//end if
                        /*---------------------------------添加爱心值-end-------------------------------------*/
                    }//end if
                }//end if
            }//end if
        }//end if
    }// end fun cancleLike()

}//end class